package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 王加鑫
 * @Date 2021/09/03 21:29
 * @Since JDK 1.8
 */
@Service(version = "1.0.0")
public class RecommendUserApiImpl implements RecommendUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserLikeApi userLikeApi;


    /**
     * 根据用户id将有缘人分最高的被推荐用户查找出来
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //根据toUserId倒序排序找出最高缘分值的有缘人
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return this.mongoTemplate.findOne(query,RecommendUser.class);
    }

    /**
     * 查出推荐用户的集合
     * @param toUserId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long toUserId, Integer pageNum, Integer pageSize) {
        //根据toUserId倒序排序找出有缘人的集合，并且按照缘分值的高低进行排序，加上分页
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)).with(Sort.by(Sort.Order.desc("score"))).with(PageRequest.of(pageNum-1,pageSize));
        List<RecommendUser> userList = this.mongoTemplate.find(query, RecommendUser.class);
        return new PageInfo<>(0,pageNum,pageSize,userList);
    }

    /**
     * 查询推荐好友的缘分值
     * @param userId 好友的id
     * @param toUserId 我的id
     * @return
     */
    @Override
    public Double queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)
                .and("userId").is(userId));
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if(ObjectUtil.isNotEmpty(recommendUser)){
            return recommendUser.getScore();
        }
        return null;
    }

    /**
     * 查询探花列表
     * @param userId
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryCardList(Long userId, Integer count) {
        //设置分页以及排序，按照得分倒叙排序
        PageRequest pageRequest = PageRequest.of(0,count,Sort.by(Sort.Order.desc("score")));

        //排除已喜欢或不喜欢的用户
        List<Long> userIds = new ArrayList<>();
        //查询喜欢列表

        userIds.addAll(this.userLikeApi.queryLikeList(userId));
        //查询不喜欢列表
        userIds.addAll(this.userLikeApi.queryNotLikeList(userId));

        //构造查询条件
        Criteria criteria = Criteria.where("toUserId").is(userId);
        if(CollUtil.isNotEmpty(userIds)){
            //加入到查询条件中，排除这些用户
            criteria.andOperator(Criteria.where("userId").nin(userIds));
        }

        Query query = Query.query(criteria).with(pageRequest);
        List<RecommendUser> recommendUserList = this.mongoTemplate.find(query, RecommendUser.class);
        if(CollUtil.isEmpty(recommendUserList)){
            return null;
        }
        return recommendUserList;
    }
}
